package org.wanda.pv.service.basic.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nutz.dao.Chain;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wanda.pv.config.FileProperties;
import org.wanda.pv.model.basic.Point;
import org.wanda.pv.service.BaseServiceImpl;
import org.wanda.pv.service.basic.PointService;

import java.util.Arrays;

/**
 * @author lxy
 * @date 2018/11/15 13:47
 */
@Service
public class PointServiceImpl extends BaseServiceImpl<Point> implements PointService {

    private final Log log = LogFactory.getLog(this.getClass());
    @Autowired
    public PointServiceImpl(Dao dao) {
        super(dao);
    }
    @Autowired
    FileProperties fileProperties;

    /**
     * 修改排序
     *
     */
    @Override
    public void updatePointLocation(Integer newLocation, Point point){
        try{
            String sqlStr1 ="";
            // 当数据在列表中向下移动的时候
            if (newLocation <point.getLocation()){
                sqlStr1 = "update wd_point set location = location+1 where location >=@newOrder and location<@oldOrder";
            }
            //把数据往下推
            if(newLocation >point.getLocation()){
                sqlStr1="update wd_point set location = location-1 where location >@oldOrder and location<=@newOrder";
            }
            //更新自身排序
            String sqlStr2 = "update wd_point set location = @newOrder where id = @id";
            Sql sql1 = Sqls.create(sqlStr1);
            Sql sql2 = Sqls.create(sqlStr2);
            sql1.setParam("newOrder",newLocation);
            sql1.setParam("oldOrder",point.getLocation());
            sql2.setParam("newOrder",newLocation);
            sql2.setParam("oldOrder",point.getLocation());
            sql2.setParam("id",point.getId());
            dao().execute(sql1,sql2);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw e;
        }
    }

    @Override
    public void del(Long id) throws Exception {
        try{
            this.delete(id);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw e;
        }
    }

    /**
     *
     * @param id
     * @param name

     * @param specialtyIds  专业类型ID数组
     * @param pointClassId 要点类型ID
     * @param pointEffectId 要点影响ID
     * @param systemId  系统ID
     * @throws Exception
     */
    @Transactional
    @Override
    public void update(Long id,
                       String name,
                       String number,
                       Long topPointId,
                       Long[] pointRoomIds,

                       Long[] specialtyIds,
                       Long pointClassId,

                       Long systemId) throws Exception {
        try{
            Sql delSql = null;
            // 不验证名称重复的问题
            Point point = this.fetch(id);
            point.setNumber(number);
            point.setName(name);
            point.setTopPointId(topPointId);
            point.setPointClassId(pointClassId);
            point.setSystemId(systemId);
            point.setDelFlag(false);


            //重新设置专业分类与题目的关联关系->清空专业分类与题目的关联关系
            delSql = Sqls.create("delete from wd_re_point_specialty where point_id = @pointId");
            delSql.setParam("pointId",id);
            dao().execute(delSql);
            if (specialtyIds!=null&&specialtyIds.length>0){
                // 设置专业分类与题目之间的关联关系
                Arrays.stream(specialtyIds).forEach(specialtyId->{
                    dao().insert("wd_re_point_specialty",Chain.make("point_id",id).add("specialty_id",specialtyId));
                });
            }




            // 重新设置题库分类与题目的关联关系-->清空题库分类与题目的关联关系
            delSql = Sqls.create("delete from wd_re_point_point_room where point_id = @pointId");
            delSql.setParam("pointId",id);
            dao().execute(delSql);
            if (pointRoomIds!=null&&pointRoomIds.length>0){
                // 设置题库分类与题目的关联关系
                Arrays.stream(pointRoomIds).forEach(pointRoomId->{
                    dao().insert("wd_re_point_point_room",Chain.make("point_id",id).add("point_room_id",pointRoomId));
                });
            }



            this.updateIgnoreNull(point);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw e;
        }

    }

    /**
     *
     * @param
     * @param name

     * @param specialtyIds  专业类型ID数组
     * @param pointClassId 要点类型ID
     * @param pointEffectId 要点影响ID
     * @param systemId  系统ID
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Point add(String name,
                     String number,
                     Long topPointId,
                        Long[] pointRoomIds,

                        Long[] specialtyIds,
                        Long pointClassId,
                        Long systemId) throws Exception{
        try {
            Point point = new Point();
            Long id = getId();
            point.setId(id);
            point.setName(name);
            point.setNumber(number);
            point.setTopPointId(topPointId);

            point.setPointClassId(pointClassId);
            point.setSystemId(systemId);

            if (specialtyIds!=null&&specialtyIds.length>0){
                // 设置专业分类与题目之间的关联关系
                Arrays.stream(specialtyIds).forEach(specialtyId->{
                    dao().insert("wd_re_point_specialty",Chain.make("point_id",id).add("specialty_id",specialtyId));
                });
            }




            if (pointRoomIds!=null&&pointRoomIds.length>0){
                // 设置题库分类与题目的关联关系
                Arrays.stream(pointRoomIds).forEach(pointRoomId->{
                    dao().insert("wd_re_point_point_room",Chain.make("point_id",id).add("point_room_id",pointRoomId));
                });
            }




            point = dao().insert(point);

            return point;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw e;
        }


    }




}
